//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.Samples.ServiceBus.SilverlightChat
{
    using System;
    using System.Windows;
    using System.Windows.Controls;

    public partial class AddTopicDialog : UserControl
    {
        private Action<AddTopicDialog, bool> callback;

        public AddTopicDialog()
        {
            // Required to initialize variables
            InitializeComponent();
        }

        /// <summary>
        /// Gets the name of the new topic.
        /// </summary>
        public string NewTopicName
        {
            get { return this.topicNameTextBox.Text; }
        }

        /// <summary>
        /// Gets a value indicating whether the 'Subscribe me to this topic' check-
        /// box is checked.
        /// </summary>
        public bool Subscribe
        {
            get { return this.subscribeCheckBox.IsChecked ?? false; }
        }

        /// <summary>
        /// Begins showing the dialog by changing the MainWindow's state to show the
        /// 'AddToTopicDialog' on top of other controls. Updates the callback used
        /// to signal completion of the dialog
        /// </summary>
        public void Show(Action<AddTopicDialog, bool> callback)
        {
            this.callback = callback;
            VisualStateManager.GoToState((Control)App.Current.RootVisual, "AddTopic", false);
        }

        /// <summary>
        /// Closes the dialog because the 'Create' button was clicked.
        /// </summary>
        private void CreateButtonClicked(object sender, RoutedEventArgs e)
        {
            // hide the dialog and call the callback specifying 'cancelled = false'
            VisualStateManager.GoToState((Control)App.Current.RootVisual, "NoDialog", false);
            this.callback(this, false);
        }

        /// <summary>
        /// Closes the dialog because the 'Cancel' button was clicked.
        /// </summary>
        private void CancelButtonClicked(object sender, RoutedEventArgs e)
        {
            // hide the dialog and call the callback specifying 'cancelled = true'
            VisualStateManager.GoToState((Control)App.Current.RootVisual, "NoDialog", false);
            this.callback(this, true);
        }
    }
}
